package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.Service.SeckillService;
import com.atguigu.gulimall.seckill.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller   //不跳转页面，使用RestController,跳转页面要使用RestController
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
        /**
         * 获取当前参与秒杀的商品信息
         */
        List<SecKillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();

        return R.ok().setData(vos);
    }

    /**
     * 查找sku的秒杀信息
     * @param skuId
     * @return
     */
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){

        SecKillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);

      return R.ok().setData(to);
    }

    /**
     *
     * @param killId skuId
     * @param key PromotionSessionId() + "_" + redis.getSkuId() redis里面存的商品的key
     * @param num 订单数量
     * @return
     */


    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                     @RequestParam("key") String key,
                      @RequestParam("num") Integer num,
                      Model model){
        String orderSn = seckillService.kill(killId,key,num);
        //秒杀成功，返回订单
        model.addAttribute("orderSn",orderSn);
        return "success.html";
    }

}
